#!/bin/bash
#最好手动部署下载flanneld里面有两个文件 flanneld mk-docker-opts.sh解压到/usr/local/kubernetes/bin
#master执行
mkdir -p /etc/cni/net.d
mkdir -p /opt/cni/bin

vi /etc/cni/net.d/10-flannel.conf
{
    "name": "cbr0",
    "type": "flannel",
    "delegate": {
    "isDefaultGateway": true
    }
}
#提前把cni包放到/opt/cni/bin目录下。
cd /opt/cni/bin && tar zxvf cni-plugins-amd64-v0.7.5.tgz
/usr/local/kubernetes/bin/etcdctl --ca-file=/usr/local/kubernetes/ssl/ca.pem --cert-file=/usr/local/kubernetes/ssl/apiserver.pem --key-file=/usr/local/kubernetes/ssl/apiserver-key.pem --endpoints="https://192.168.31.121:2379" set /coreos.com/network/config  '{ "Network": "172.17.0.0/16", "Backend": {"Type": "vxlan"}}'
ETCD_ENDPOINTS=${1:-"https://192.168.31.121:2379"}

#node执行
cat <<EOF >/usr/local/kubernetes/cfg/flanneld
FLANNEL_OPTIONS="--etcd-endpoints=${ETCD_ENDPOINTS} \
-etcd-cafile=/usr/local/kubernetes/ssl/ca.pem \
-etcd-certfile=/usr/local/kubernetes/ssl/apiserver.pem \
-etcd-keyfile=/usr/local/kubernetes/ssl/apiserver-key.pem"
EOF

#node执行
cat <<EOF >/usr/lib/systemd/system/flanneld.service
[Unit]
Description=Flanneld overlay address etcd agent
After=network-online.target network.target
Before=docker.service

[Service]
Type=notify
EnvironmentFile=/usr/local/kubernetes/cfg/flanneld
ExecStart=/usr/local/kubernetes/bin/flanneld --ip-masq $FLANNEL_OPTIONS
ExecStartPost=/usr/local/kubernetes/bin/mk-docker-opts.sh -k DOCKER_NETWORK_OPTIONS -d /run/flannel/docker
Restart=on-failure

[Install]
WantedBy=multi-user.target

EOF
#node执行
cat <<EOF >/usr/lib/systemd/system/docker.service

[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target

[Service]
Type=notify
EnvironmentFile=/run/flannel/subnet.env
ExecStart=/usr/bin/dockerd  $DOCKER_NETWORK_OPTIONS
ExecReload=/bin/kill -s HUP $MAINPID
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
TimeoutStartSec=0
Delegate=yes
KillMode=process
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s

[Install]
WantedBy=multi-user.target

EOF

systemctl daemon-reload
systemctl enable flanneld
systemctl restart flanneld
systemctl restart docker
